package com.luoshanguo.admin.controller;


import com.luoshanguo.admin.exceptions.ParamsException;
import com.luoshanguo.admin.model.RespBean;
import com.luoshanguo.admin.pojo.User;
import com.luoshanguo.admin.service.IUserService;
import com.luoshanguo.admin.utils.AssertUtil;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 罗某人
 * @since 2021-08-06
 */
@Controller
@RequestMapping("/user")
public class UserController {


    @Resource
    private IUserService iUserService;


    /**
     * 用户登录
     */
    @RequestMapping("login")
    @ResponseBody
    public RespBean login(String userName, String password, HttpSession httpSession){
            //调用service方法
            User user = iUserService.login(userName,password);
            //把用户信息存入session中
            httpSession.setAttribute("user",user);
            return RespBean.success("用户登录成功");
    }

    /**
     * 用户信息展示
     * @return
     */
    @RequestMapping("setting")
    public String setting(HttpSession httpSession){
        User user = (User)httpSession.getAttribute("user");
        httpSession.setAttribute("user",iUserService.getById(user.getId()));
        return "user/setting";
    }

    /**
     * 用户信息更新
     * @param user
     * @return
     */
    @RequestMapping("updateUserInfo")
    @ResponseBody
    public RespBean updateUserInfo(User user){
            iUserService.updateUserInfo(user);
            return RespBean.success("用户信息更新成功");
    }

    /**
     * 跳转修改密码页面
     * @return
     */
    @RequestMapping("password")
    public String password(){
        return "user/password";
    }

    /**
     * 密码更新
     * @return
     */
    @RequestMapping("updateUserPassword")
    @ResponseBody
    public RespBean updateUserPassword(HttpSession httpSession,String oldPassword,
                                       String newPassword,String confirmPassword){
            User user = (User)httpSession.getAttribute("user");
            iUserService.updateUserPassword(user.getUserName(),oldPassword,newPassword,confirmPassword);
            return RespBean.success("用户密码更新成功");
    }
}
